/**
 * 定义路由： Router.[method](path, (request, response) => { })
 *    Router: 路由实例
 *    app.method(path, (request, response) => { })
 */
// 使用 multer 处理文件上传
const multer = require("multer");
const { v4: uuid } = require("uuid");
const path = require("path");
// 配置上传文件的存储位置和文件名
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, "./public/avatars");
    },
    filename: function (req, file, cb) {
        const ext = path.extname(file.originalname);
        cb(null, uuid() + ext);
    },
});
const upload = multer({ storage });

const { db } = require("../database");

const { validate, validators } = require("../validate");

// 导入加密函数
const { makePassword, checkPassword } = require("../utils");
// jwt
const { sign } = require("../jwt");
const multiAvatar = require("@multiavatar/multiavatar");

const router = require("express").Router();
const { useAuthorization}  = require("./base");
router.use(useAuthorization); //You，13小时前。用户

//获取模块信息
router.get(
    '/module',
    async (req, res) => {
        const module = await db.select('select * from module')
        res.send(
            {
                success: true,
                message: "获取模块信息成功",
                object: module,
            }
        )
    }
)
router.get(
    '/module/select',
    async (req, res) => {
        const {name}=req.body
        const module = await db.select('select * from module where name=?',name)
        res.send(
            {
                success: true,
                message: "获取模块信息成功",
                object: module,
            }
        )
    }
)
//新增模块
router.post(
    '/module',
  
    async (req, res) => {
        const { name, sn, url, orderNo, parent, children } = req.body;
        //判断是否已存在该模块
        const user = await db.findOne("select * from module where name = ?", [
            name,
        ]);
        if (user) {
            res.send({ success: false, message: "该模块已存在" })
        }
        // 插入模块信息
        const id = await db.insert(
            "insert into module (name, sn, url, orderNo,parent,children) values (?, ?, ?, ?, ?, ?)",
            [name, sn, url, orderNo, parent, children]
        );
        // 查询刚刚插入的模块信息
        const module = await db.findOne`select * from module where id = ${id}`;
        res.send({
            success: true,
            message: "新增模块成功",
            object: module,
        });
    }
)
//删除模块
router.delete('/module',
    async (req, res) => {
        const { id } = req.body;
        const module = await db.run`delete from module where id = ${id}`;
        if (!module) {
            res.send({ success: true, message: "模块删除成功" })
        }
       
    }
)
//修改模块
router.put(
    '/module',
  
    async (req, res) => {
        const { id, name, sn, url, orderNo, parent, children } = req.body;
        if (!id) {
            res.send({ success: false, message: "该模块不存在" })
        }
        else {
            await db.run`update module set name = ${name}, sn = ${sn}, url = ${url},orderNo = ${orderNo}, parent = ${parent}, children = ${children} where id = ${id}`;
            // 查询刚刚修改的模块信息
            const user1 = await db.findOne`select * from module where id = ${id}`;
            res.send({
                success: true,
                message: "修改模块成功",
                object: user1,
            });
        }
    }
)




const registerRouter = (app) => app.use("/module", router);
module.exports = { registerRouter };